{% set pypi_name = 'glance' %}
{% set source = fetch_source('https://tarballs.openstack.org/glance/glance-master.tar.gz') %}
{% set upstream_version = upstream_version() %}
{% set rpm_release = '1' %}
%global with_doc 1
%if 0%{?rhel} || 0%{?fedora}
%global rdo 1
%endif
Name:           {{ py2name() }}
Version:        {{ py2rpmversion() }}
Release:        {{ py2rpmrelease() }}
Epoch:          {{ epoch('glance') }}
Summary:        OpenStack Image Service (Glance)
License:        {{ license('Apache-2.0') }}
URL:            https://docs.openstack.org/glance
Source0:        {{ source|basename }}
Source1:        openstack-glance.logrotate
Source2:        openstack-glance.conf
Source3:        openstack-glance-api.service
Source6:        openstack-glance.defaultconf
Source7:        openstack-glance.README.config
BuildRequires:  openstack-macros
BuildRequires:  {{ py3('Paste') }}
BuildRequires:  {{ py3('PasteDeploy') }}
BuildRequires:  {{ py3('Routes') }}
BuildRequires:  {{ py3('WSME') }}
BuildRequires:  {{ py3('WebOb') }}
BuildRequires:  {{ py3('cursive') }}
BuildRequires:  {{ py3('ddt') }}
BuildRequires:  {{ py3('debtcollector') }}
BuildRequires:  {{ py3('defusedxml') }}
BuildRequires:  {{ py3('eventlet') }}
BuildRequires:  {{ py3('futurist') }}
BuildRequires:  {{ py3('glance_store') }}
BuildRequires:  {{ py3('httplib2') }}
BuildRequires:  {{ py3('keystoneclient') }}
BuildRequires:  {{ py3('keystonemiddleware') }}
BuildRequires:  {{ py3('oslo.concurrency') }}
BuildRequires:  {{ py3('oslo.config') }}
BuildRequires:  {{ py3('oslo.context') }}
BuildRequires:  {{ py3('oslo.db') }}
BuildRequires:  {{ py3('oslo.log') }}
BuildRequires:  {{ py3('oslo.messaging') }}
BuildRequires:  {{ py3('oslo.middleware') }}
BuildRequires:  {{ py3('oslo.policy') }}
BuildRequires:  {{ py3('oslo.reports') }}
BuildRequires:  {{ py3('oslo.upgradecheck') }}
BuildRequires:  {{ py3('oslo.utils') }}
BuildRequires:  {{ py3('oslotest') }}
BuildRequires:  {{ py3('osprofiler') }}
BuildRequires:  {{ py3('pyOpenSSL') }}
BuildRequires:  {{ py3('retrying') }}
BuildRequires:  {{ py3('sqlparse') }}
BuildRequires:  {{ py3('stestr') }}
BuildRequires:  {{ py3('stevedore') }}
BuildRequires:  {{ py3('taskflow') }}
BuildRequires:  {{ py3('xattr') }}
Requires:       logrotate
Requires:       python3-{{ pypi_name }} = %{epoch}:%{version}-%{release}
BuildArch:      noarch
%if 0%{?suse_version}
BuildRequires:  systemd-rpm-macros
%{?systemd_requires}
%else
BuildRequires:  systemd
Requires(post): systemd
Requires(postun): systemd
Requires(preun): systemd
%endif
%if 0%{?suse_version}
BuildRequires:  qemu-tools
Requires(pre):  pwdutils
%endif
%if 0%{?rdo}
Requires(pre):  shadow-utils
%endif

%description
The Glance project provides a service where users can upload and discover
data assets that are meant to be used with other services. This currently
includes images and metadata definitions.
Glance image services include discovering, registering, and retrieving
virtual machine images. Glance has a RESTful API that allows querying of
VM image metadata as well as retrieval of the actual image.
VM images made available through Glance can be stored in a variety of
locations from simple filesystems to object-storage systems like the
OpenStack Swift project.

%package -n     python3-{{ pypi_name }}
Summary:        OpenStack Image Service (Glance) - Python module
Group:          Applications/System
Requires:       {{ py3('Paste') }}
Requires:       {{ py3('PasteDeploy') }}
Requires:       {{ py3('PrettyTable') }}
Requires:       {{ py3('Routes') }}
Requires:       {{ py3('SQLAlchemy') }}
Requires:       {{ py3('WSME') }}
Requires:       {{ py3('WebOb') }}
Requires:       {{ py3('alembic') }}
Requires:       {{ py3('cryptography') }}
Requires:       {{ py3('cursive') }}
Requires:       {{ py3('debtcollector') }}
Requires:       {{ py3('defusedxml') }}
Requires:       {{ py3('eventlet') }}
Requires:       {{ py3('futurist') }}
Requires:       {{ py3('glance_store') }}
Requires:       {{ py3('httplib2') }}
Requires:       {{ py3('iso8601') }}
Requires:       {{ py3('jsonschema') }}
Requires:       {{ py3('keystoneauth1') }}
Requires:       {{ py3('python-keystoneclient') }}
Requires:       {{ py3('keystonemiddleware') }}
Requires:       {{ py3('monotonic') }}
Requires:       {{ py3('oslo.concurrency') }}
Requires:       {{ py3('oslo.config') }}
Requires:       {{ py3('oslo.context') }}
Requires:       {{ py3('oslo.db') }}
Requires:       {{ py3('oslo.i18n') }}
Requires:       {{ py3('oslo.log') }}
Requires:       {{ py3('oslo.messaging') }}
Requires:       {{ py3('oslo.middleware') }}
Requires:       {{ py3('oslo.policy') }}
Requires:       {{ py3('oslo.reports') }}
Requires:       {{ py3('oslo.upgradecheck') }}
Requires:       {{ py3('oslo.utils') }}
Requires:       {{ py3('osprofiler') }}
Requires:       {{ py3('pbr') }}
Requires:       {{ py3('pyOpenSSL') }}
Requires:       {{ py3('retrying') }}
Requires:       {{ py3('six') }}
Requires:       {{ py3('sqlalchemy-migrate') }}
Requires:       {{ py3('sqlparse') }}
Requires:       {{ py3('stevedore') }}
Requires:       {{ py3('taskflow') }}

%description -n python3-{{ pypi_name }}
The Glance project provides a service where users can upload and discover
data assets that are meant to be used with other services. This currently
includes images and metadata definitions.
Glance image services include discovering, registering, and retrieving
virtual machine images. Glance has a RESTful API that allows querying of
VM image metadata as well as retrieval of the actual image.
VM images made available through Glance can be stored in a variety of
locations from simple filesystems to object-storage systems like the
OpenStack Swift project.

This package contains the Glance Python library.

%package -n python3-{{ pypi_name }}-tests
Summary:        Glance tests
Requires:       openstack-{{ pypi_name }} = %{epoch}:%{version}-%{release}
Provides:       python-glance-tests = %{epoch}:%{version}-%{release}

%description -n python3-{{ pypi_name }}-tests
The Glance project provides a service where users can upload and discover
data assets that are meant to be used with other services. This currently
includes images and metadata definitions.
Glance image services include discovering, registering, and retrieving
virtual machine images. Glance has a RESTful API that allows querying of
VM image metadata as well as retrieval of the actual image.
VM images made available through Glance can be stored in a variety of
locations from simple filesystems to object-storage systems like the
OpenStack Swift project.

This package contains the Glance test files.

%if 0%{?with_doc}
%package doc
Summary:        Documentation for OpenStack Image Service
Group:          Documentation
BuildRequires:  {{ py3('Sphinx') }}
BuildRequires:  {{ py3('openstackdocstheme') }}
BuildRequires:  {{ py3('sphinxcontrib-apidoc') }}

%description doc
The Glance project provides a service where users can upload and discover
data assets that are meant to be used with other services. This currently
includes images and metadata definitions.
Glance image services include discovering, registering, and retrieving
virtual machine images. Glance has a RESTful API that allows querying of
VM image metadata as well as retrieval of the actual image.
VM images made available through Glance can be stored in a variety of
locations from simple filesystems to object-storage systems like the
OpenStack Swift project.

This package contains the documentation.
%endif

%package api
Summary:        OpenStack Image service (Glance) - API
Group:          Development/Languages/Python
Requires:       %{name} = %{epoch}:%{version}-%{release}

%description api
The Glance project provides a service where users can upload and discover
data assets that are meant to be used with other services. This currently
includes images and metadata definitions.
Glance image services include discovering, registering, and retrieving
virtual machine images. Glance has a RESTful API that allows querying of
VM image metadata as well as retrieval of the actual image.
VM images made available through Glance can be stored in a variety of
locations from simple filesystems to object-storage systems like the
OpenStack Swift project.

This package contains the %{name} API server.

%prep
%autosetup -p1 -n {{ pypi_name }}-{{ upstream_version }}
%py_req_cleanup

%build
%{py3_build}
# build documentation
%if 0%{?with_doc}
PBR_VERSION={{ upstream_version }} %sphinx_build -b html doc/source doc/build/html
PBR_VERSION={{ upstream_version }} %sphinx_build -b man doc/source doc/build/man
rm -rf doc/build/html/.{doctrees,buildinfo}
rm -rf doc/build/man/.{doctrees,buildinfo}
%endif
# regenerate the sample config files
for service in api scrubber cache manage image-import; do
    PYTHONPATH=. oslo-config-generator --config-file etc/oslo-config-generator/glance-$service.conf
done
# generate the sample policy
PYTHONPATH=. oslopolicy-sample-generator --config-file=etc/glance-policy-generator.conf --output etc/policy.json
rm -f etc/glance-policy-generator.conf

%install
%{py3_install}
# directories
install -d -m 755 %{buildroot}%{_sysconfdir}/{{ pypi_name }}
install -d -m 755 %{buildroot}%{_sysconfdir}/glance/glance.conf.d/
install -d -m 755 %{buildroot}%{_sysconfdir}/glance/glance-api.conf.d/
install -d -m 755 %{buildroot}%{_localstatedir}/lib/{{ pypi_name }}
install -d -m 755 %{buildroot}%{_localstatedir}/lib/{{ pypi_name }}/images
install -d -m 750 %{buildroot}%{_localstatedir}/log/{{ pypi_name }}
install -d -m 750 %{buildroot}%{_localstatedir}/cache/{{ pypi_name }}
install -d -m 700 %{buildroot}%{_rundir}/{{ pypi_name }}
# tmpfile generator
install -D -m 644 %{SOURCE2} %{buildroot}/%{_tmpfilesdir}/glance.conf
# install sample configs
cp -pr etc/* %{buildroot}%{_sysconfdir}/{{ pypi_name }}
for service in api scrubber cache manage swift image-import; do
    mv %{buildroot}%{_sysconfdir}/{{ pypi_name }}/glance-$service.conf{.sample,}
done
install -p -D -m 640 %{SOURCE6} %{buildroot}%{_sysconfdir}/{{ pypi_name }}/README.config
rm -rf %{buildroot}%{_sysconfdir}/{{ pypi_name }}/oslo-config-generator
# default config
install -D -m 640 %{SOURCE6} %{buildroot}/%{_sysconfdir}/glance/glance.conf.d/010-glance.conf
# systemd
install -p -D -m 444 %{SOURCE3} %{buildroot}%{_unitdir}/openstack-glance-api.service
# logrotate
install -p -D -m 644 %{SOURCE1} %{buildroot}%{_sysconfdir}/logrotate.d/openstack-glance
# move metadefs
mv %{buildroot}%{_prefix}%{_sysconfdir}/glance/metadefs/* %{buildroot}%{_sysconfdir}/glance/metadefs
# Remove duplicate files under /usr/etc
rm -rf %{buildroot}%{_prefix}%{_sysconfdir}/glance

# man pages
%if 0%{?with_doc}
mkdir -p %{buildroot}%{_mandir}/man1
install -p -D -m 644 doc/build/man/*.1 %{buildroot}%{_mandir}/man1/
%endif

%pre
%openstack_pre_user_group_create glance glance /sbin/nologin

%check
%if 0%{?rdo}
# Skip qemu-tools related tests, those fail on RDO due to missing VDI support
rm -v glance/tests/unit/common/test_format_inspector.py
%endif
PYTHONPATH=. python3 -m stestr.cli --test-path glance/tests/unit run

%post
%tmpfiles_create %{_tmpfilesdir}/glance.conf

%post api
%systemd_post %{name}-api.service

%preun api
%systemd_preun %{name}-api.service

%postun api
%systemd_postun_with_restart %{name}-api.service

%files
%license LICENSE
%_tmpfilesdir/glance.conf
%dir %attr(0750, root, glance) %{_sysconfdir}/{{ pypi_name }}
%dir %attr(0750, root, glance) %{_sysconfdir}/glance/glance.conf.d/
%dir %attr(0750, root, glance) %{_sysconfdir}/glance/glance-api.conf.d/
%config(noreplace) %attr(0640, root, glance) %{_sysconfdir}/glance/glance.conf.d/010-glance.conf
%{_sysconfdir}/{{ pypi_name }}/README.config
%config(noreplace) %attr(0640, root, glance) %{_sysconfdir}/{{ pypi_name }}/glance-api.conf
%config(noreplace) %attr(0640, root, glance) %{_sysconfdir}/{{ pypi_name }}/glance-cache.conf
%config(noreplace) %attr(0640, root, glance) %{_sysconfdir}/{{ pypi_name }}/glance-image-import.conf
%config(noreplace) %attr(0640, root, glance) %{_sysconfdir}/{{ pypi_name }}/glance-scrubber.conf
%config(noreplace) %attr(0640, root, glance) %{_sysconfdir}/{{ pypi_name }}/glance-swift.conf
%config(noreplace) %attr(0640, root, glance) %{_sysconfdir}/{{ pypi_name }}/glance-manage.conf
%config %{_sysconfdir}/{{ pypi_name }}/glance-api-paste.ini
%config %{_sysconfdir}/{{ pypi_name }}/policy.json
%config %{_sysconfdir}/{{ pypi_name }}/property-protections-policies.conf.sample
%config %{_sysconfdir}/{{ pypi_name }}/property-protections-roles.conf.sample
%config %{_sysconfdir}/{{ pypi_name }}/ovf-metadata.json.sample
%config %{_sysconfdir}/{{ pypi_name }}/schema-image.json
%dir %config %{_sysconfdir}/{{ pypi_name }}/metadefs
%{_sysconfdir}/{{ pypi_name }}/metadefs/README
%config %{_sysconfdir}/{{ pypi_name }}/metadefs/*.json
%config(noreplace) %{_sysconfdir}/logrotate.d/%{name}
%dir %attr(0755, glance, glance) %{_localstatedir}/lib/{{ pypi_name }}
%dir %attr(0755, glance, glance) %{_localstatedir}/lib/{{ pypi_name }}/images
%dir %attr(0750, glance, glance) %{_localstatedir}/log/{{ pypi_name }}
%dir %attr(0750, glance, glance) %{_localstatedir}/cache/{{ pypi_name }}
%{_bindir}/glance-cache-cleaner
%{_bindir}/glance-cache-manage
%{_bindir}/glance-cache-prefetcher
%{_bindir}/glance-cache-pruner
%{_bindir}/glance-control
%{_bindir}/glance-manage
%{_bindir}/glance-replicator
%{_bindir}/glance-scrubber
%{_bindir}/glance-status
%if 0%{?with_doc}
%{_mandir}/man1/glance-cache-cleaner.1.gz
%{_mandir}/man1/glance-cache-manage.1.gz
%{_mandir}/man1/glance-cache-prefetcher.1.gz
%{_mandir}/man1/glance-cache-pruner.1.gz
%{_mandir}/man1/glance-control.1.gz
%{_mandir}/man1/glance-manage.1.gz
%{_mandir}/man1/glance-replicator.1.gz
%{_mandir}/man1/glance-scrubber.1.gz
%endif

%files -n python3-glance
%license LICENSE
%{python3_sitelib}/{{ pypi_name }}*
%exclude %{python3_sitelib}/glance/tests

%files -n python3-{{ pypi_name }}-tests
%license LICENSE
%{python3_sitelib}/{{ pypi_name }}/tests

%if 0%{?with_doc}
%files doc
%license LICENSE
%doc doc/build/html
%endif

%files api
%{_unitdir}/openstack-glance-api.service
%{_bindir}/{{ pypi_name }}-api
%{_bindir}/{{ pypi_name }}-wsgi-api
%if 0%{?with_doc}
%{_mandir}/man1/glance-api.1.gz
%endif

%changelog
